﻿Imports System.Web.UI.WebControls
Imports System.Web.UI

Namespace DotNetNuke.Modules.ActiveForums
    Partial Public Class WhatsNewOptions
        Inherits DotNetNuke.Entities.Modules.PortalModuleBase
        Public Sub New()
      
        End Sub
#Region "Event Handlers"
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Page.ClientScript.RegisterClientScriptInclude("afutils", Page.ResolveUrl("~/DesktopModules/ActiveForums/scripts/afutils.js"))
            If Not Page.IsPostBack Then
                LoadForm()
            End If
        End Sub
        'Private Sub tsTags_Callback(ByVal sender As Object, ByVal e As Modules.ActiveForums.Controls.CallBackEventArgs) Handles tsTags.Callback
        '    tsTags.Datasource = DataProvider.Instance.Tags_Search(PortalId, ModuleId, e.Parameter.ToString + "%")
        '    tsTags.Refresh(e.Output)
        'End Sub
        Private Sub lnkUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkUpdate.Click
            Try
                Dim objModules As New DotNetNuke.Entities.Modules.ModuleController

                ' Update Settings

                ' Construct Forums 
                Dim f As String = String.Empty
                If Not GetNodeCount() = trForums.CheckedNodes.Count Then
                    If trForums.CheckedNodes.Count > 1 Then
                        f = ""
                        For Each tr As TreeNode In trForums.CheckedNodes
                            If tr.Value.Contains("F:") Then
                                f &= tr.Value.Split(CChar(":"))(1) & ":"
                            End If
                        Next
                    Else
                        Dim sv As String = trForums.CheckedNodes.Item(0).Value
                        If sv.Contains("F:") Then
                            f = sv.Split(CChar(":"))(1)
                        End If
                    End If
                End If
                objModules.UpdateModuleSetting(ModuleId, "AFTopPostsForums", f)
                objModules.UpdateModuleSetting(ModuleId, "AFTopPostsNumber", txtNumItems.Text)
                objModules.UpdateModuleSetting(ModuleId, "AFTopPostsFormat", txtTemplate.Text)
                objModules.UpdateModuleSetting(ModuleId, "AFTopPostsHeader", txtHeader.Text)
                objModules.UpdateModuleSetting(ModuleId, "AFTopPostsFooter", txtFooter.Text)
                objModules.UpdateModuleSetting(ModuleId, "AFTopPostsRSS", chkRSS.Checked.ToString)
                objModules.UpdateModuleSetting(ModuleId, "AFTopPostsTopicsOnly", chkTopicsOnly.Checked.ToString)
                objModules.UpdateModuleSetting(ModuleId, "AFTopPostsRandomOrder", chkRandomOrder.Checked.ToString)
                objModules.UpdateModuleSetting(ModuleId, "AFTopPostsTags", txtTags.Text)

                If chkRSS.Checked = True Then
                    objModules.UpdateModuleSetting(ModuleId, "AFTopPostsSecurity", chkIgnoreSecurity.Checked.ToString)
                    objModules.UpdateModuleSetting(ModuleId, "AFTopPostsBody", chkIncludeBody.Checked.ToString)
                    objModules.UpdateModuleSetting(ModuleId, "AFTopPostsCache", txtCache.Text.ToString)
                End If


                ' Redirect back to the portal home page
                DataCache.CacheClear("aftp" & ModuleId)
                Response.Redirect(DotNetNuke.Common.NavigateURL(), True)
            Catch exc As Exception
                DotNetNuke.Services.Exceptions.ProcessModuleLoadException(Me, exc)
            End Try
        End Sub
#End Region
#Region "Private Methods"
        Private Function GetNodeCount() As Integer
            Dim nc As Integer = 0
            nc += trForums.Nodes.Count
            For Each node As TreeNode In trForums.Nodes
                nc += 1
                If node.ChildNodes.Count > 0 Then
                    nc += node.ChildNodes.Count
                    For Each cnode As TreeNode In node.ChildNodes
                        nc += cnode.ChildNodes.Count
                        If cnode.ChildNodes.Count > 0 Then
                            For Each subnode As TreeNode In cnode.ChildNodes
                                nc += cnode.ChildNodes.Count
                            Next
                        End If
                    Next
                End If
            Next
        End Function
        Private Sub LoadForm()
            Dim settings As Hashtable = DotNetNuke.Entities.Portals.PortalSettings.GetModuleSettings(ModuleId)
            Dim forumids As String = ""
            If settings Is Nothing Then
                txtHeader.Text = "<div style=""padding:25px;padding-top:35px;"">"
                txtTemplate.Text = "<div style=""padding-bottom:5px;"" class=""normal"">[SUBJECTLINK]</div>"
                txtFooter.Text = "[RSSICONLINK]</div>"
                txtCache.Text = "10"
            ElseIf settings.Count = 0 Then
                txtHeader.Text = "<div style=""padding:25px;padding-top:35px;"">"
                txtTemplate.Text = "<div style=""padding-bottom:5px;"" class=""normal"">[SUBJECTLINK]</div>"
                txtFooter.Text = "[RSSICONLINK]</div>"
                txtCache.Text = "10"
            Else
                If Not CType(settings("AFTopPostsForums"), String) Is Nothing Then
                    forumids = CType(settings("AFTopPostsForums"), String)
                End If
                If Not CType(settings("AFTopPostsNumber"), String) Is Nothing Then
                    txtNumItems.Text = CType(settings("AFTopPostsNumber"), String)
                End If
                If Not CType(settings("AFTopPostsFormat"), String) Is Nothing Then
                    txtTemplate.Text = CType(settings("AFTopPostsFormat"), String)
                End If
                If Not CType(settings("AFTopPostsHeader"), String) Is Nothing Then
                    txtHeader.Text = CType(settings("AFTopPostsHeader"), String)
                End If
                If Not CType(settings("AFTopPostsFooter"), String) Is Nothing Then
                    txtFooter.Text = CType(settings("AFTopPostsFooter"), String)
                End If
                If Not CType(settings("AFTopPostsRSS"), String) Is Nothing Then
                    chkRSS.Checked = CType(settings("AFTopPostsRSS"), Boolean)
                    trRSS.Visible = CType(settings("AFTopPostsRSS"), Boolean)
                End If
                If Not CType(settings("AFTopPostsTopicsOnly"), String) Is Nothing Then
                    chkTopicsOnly.Checked = CType(settings("AFTopPostsTopicsOnly"), Boolean)
                End If
                If Not CType(settings("AFTopPostsTags"), String) Is Nothing Then
                    txtTags.Text = CType(settings("AFTopPostsTags"), String)
                End If
                If Not CType(settings("AFTopPostsRandomOrder"), String) Is Nothing Then
                    chkRandomOrder.Checked = CType(settings("AFTopPostsRandomOrder"), Boolean)
                End If
                If Not CType(settings("AFTopPostsSecurity"), String) Is Nothing Then
                    chkIgnoreSecurity.Checked = CType(settings("AFTopPostsSecurity"), Boolean)
                End If
                If Not CType(settings("AFTopPostsBody"), String) Is Nothing Then
                    chkIncludeBody.Checked = CType(settings("AFTopPostsBody"), Boolean)
                End If
                If Not CType(settings("AFTopPostsCache"), String) Is Nothing Then
                    txtCache.Text = CType(settings("AFTopPostsCache"), String)
                End If
            End If

            BindForumsTree()
            If Not forumids = String.Empty Then
                CheckNodes(forumids)
            End If





        End Sub
        Private Sub CheckNodes(ByVal ForumList As String)
            Dim Forums() As String = Nothing
            Forums = ForumList.Split(CChar(":"))
            If Not Forums Is Nothing Then
                'Clear all Nodes
                ManageCheck(False)
                If Not Forums Is Nothing Then
                    For Each f As String In Forums
                        If Not f.Trim = "" Then
                            ManageCheck(False, "F:" & f)
                        End If
                    Next
                End If
            End If

        End Sub
        Private Sub ManageCheck(ByVal state As Boolean, Optional ByVal value As String = "")
            For Each node As TreeNode In trForums.Nodes
                If Not node.Checked Then
                    node.Checked = CBool(IIf(node.Value = value, True, state))
                    If node.Checked Then
                        node.Parent.Expanded = True
                    End If
                End If
                If node.ChildNodes.Count > 0 Then
                    For Each cnode As TreeNode In node.ChildNodes
                        If Not cnode.Checked Then
                            cnode.Checked = CBool(IIf(cnode.Value = value, True, state))
                            If cnode.Checked Then
                                cnode.Parent.Expanded = True
                            End If
                        End If
                        If cnode.ChildNodes.Count > 0 Then
                            For Each subnode As TreeNode In cnode.ChildNodes
                                If Not subnode.Checked Then
                                    subnode.Checked = CBool(IIf(subnode.Value = value, True, state))
                                    If subnode.Checked Then
                                        subnode.Parent.Expanded = True
                                    End If
                                End If

                            Next
                        End If
                    Next
                End If
            Next

        End Sub

        Private Sub BindForumsTree()

            Dim trNodes As New TreeNodeCollection
            Dim trGroupNode As TreeNode = Nothing
            Dim trParentNode As TreeNode = Nothing
            Dim trNode As TreeNode = Nothing
            Dim reader As IDataReader = Nothing
            Dim dt As New DataTable("Forums")
            Dim row As DataRow
            dt = New DataTable
            dt.Load(DataProvider.Instance.PortalForums(PortalId))


            Dim tmpGroup As String = String.Empty
            Dim i As Integer = 0
            For Each row In dt.Rows
                If tmpGroup <> row("ForumGroupId").ToString Then
                    trGroupNode = New TreeNode
                    trGroupNode.Text = row("GroupName").ToString
                    trGroupNode.ImageUrl = "~/DesktopModules/ActiveForums/images/tree/tree_group.png"
                    trGroupNode.ShowCheckBox = True
                    trGroupNode.SelectAction = TreeNodeSelectAction.None
                    trGroupNode.Value = "G:" & row("ForumGroupId").ToString
                    If i = 0 Then
                        trGroupNode.Expanded = True
                    Else
                        trGroupNode.Expanded = False
                    End If
                    i += 1
                    tmpGroup = row("ForumGroupId").ToString
                    trNodes.Add(trGroupNode)
                End If
                If CInt(row("ParentForumId")) = 0 Then
                    trNode = New TreeNode
                    trNode.Text = row("ForumName").ToString
                    trNode.ImageUrl = "~/DesktopModules/ActiveForums/images/tree/tree_forum.png"
                    trNode.ShowCheckBox = True
                    trNode.Expanded = False
                    trNode.SelectAction = TreeNodeSelectAction.None
                    trNode.Value = "F:" & row("ForumId").ToString
                    If HasSubForums(CInt(row("ForumId")), dt) Then
                        AddChildNodes(trNode, dt, row)
                    End If
                    'If trNode.ChildNodes.Count > 0 Then
                    trGroupNode.ChildNodes.Add(trNode)
                    'End If

                End If

            Next
            Dim tr As TreeNode
            For Each tr In trNodes
                If tr.ChildNodes.Count > 0 Then
                    trForums.Nodes.Add(tr)
                End If
            Next


        End Sub
        Private Sub AddChildNodes(ByVal ParentNode As TreeNode, ByVal dt As DataTable, ByVal dr As DataRow)
            Dim tNode As TreeNode = Nothing
            Dim row As DataRow = Nothing
            For Each row In dt.Rows
                If CInt(dr("ForumId")) = CInt(row("ParentForumId")) Then
                    tNode = New TreeNode
                    tNode.Text = row("ForumName").ToString
                    tNode.ImageUrl = "~/DesktopModules/ActiveForums/images/tree/tree_forum.png"
                    tNode.ShowCheckBox = True
                    tNode.Value = "F:" & row("ForumId").ToString
                    tNode.Checked = False
                    tNode.SelectAction = TreeNodeSelectAction.None
                    ParentNode.ChildNodes.Add(tNode)
                    AddChildNodes(tNode, dt, row)
                End If
            Next
        End Sub
        Private Function HasSubForums(ByVal ForumId As Integer, ByVal dt As DataTable) As Boolean
            Dim row As DataRow = Nothing
            For Each row In dt.Rows
                If CInt(row("ParentForumId")) = ForumId Then
                    Return True
                End If
            Next
            Return False
        End Function
        Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
            Dim stringWriter As System.IO.StringWriter = New System.IO.StringWriter
            Dim htmlWriter As HtmlTextWriter = New HtmlTextWriter(stringWriter)
            MyBase.Render(htmlWriter)
            Dim html As String = stringWriter.ToString()
            html = Utilities.LocalizeControl(html, "WhatsNew.ascx")
            writer.Write(html)
        End Sub

#End Region

    End Class
End Namespace
